<html>
<head><meta charset="utf-8"><title>Accessing symbols from nasm · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html">Accessing symbols from nasm</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="179410023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179410023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179410023">(Oct 30 2019 at 08:10)</a>:</h4>
<p>in rav1e we tried to define tables in rust and then use them from nasm, when not using cdylibs on linux everything seems fine, on linux+cdylib we got this:</p>
<div class="codehilite"><pre><span></span>  = note: /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /home/lu_zero/Sources/rust/rav1e/target/debug/build/rav1e-646b21e8f2fef3cb/out/librav1easm.a(ipred.o): warning: relocation against `rav1e_filter_intra_taps&#39; in readonly section `.text&#39;
          /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /home/lu_zero/Sources/rust/rav1e/target/debug/build/rav1e-646b21e8f2fef3cb/out/librav1easm.a(ipred.o): relocation R_X86_64_PC32 against symbol `rav1e_dr_intra_derivative&#39; can not be used when making a shared object; recompile with -fPIC
</pre></div>


<p>anybody has ideas on how to fix it?</p>



<a name="179411037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179411037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179411037">(Oct 30 2019 at 08:29)</a>:</h4>
<p>setting <code>relocation-model=pic</code> does doesn't change anything</p>



<a name="179412610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179412610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179412610">(Oct 30 2019 at 09:01)</a>:</h4>
<p>Try using <code>symbol@plt</code> instead of <code>symbol</code> in your asm code.</p>



<a name="179412917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179412917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179412917">(Oct 30 2019 at 09:05)</a>:</h4>
<p>Oh wait, that's a data table, not a function.</p>



<a name="179413139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179413139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179413139">(Oct 30 2019 at 09:09)</a>:</h4>
<p><span class="user-mention" data-user-id="131070">@Luca Barbato</span> Apparently the correct syntax for obtaining the address of a PC-relative symbol is <code>lea rax, [rip + symbol]</code>. Can you try that to see if it works?</p>



<a name="179413754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179413754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179413754">(Oct 30 2019 at 09:18)</a>:</h4>
<p>the code using it uses <code>    lea                  r7, [dr_intra_derivative]</code> and I'd rather not change it since it is shared with another project ^^</p>



<a name="179414434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179414434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179414434">(Oct 30 2019 at 09:27)</a>:</h4>
<p><span class="user-mention" data-user-id="131070">@Luca Barbato</span> Actually, can you double-check that the symbols that are referenced are from the same shared library?</p>



<a name="179414495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179414495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179414495">(Oct 30 2019 at 09:28)</a>:</h4>
<p>let me explain the whole scenario</p>



<a name="179414498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179414498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179414498">(Oct 30 2019 at 09:28)</a>:</h4>
<p>From the error message, the linker seems to think that rav1e_filter_intra_taps is a symbol defined outside the library.</p>



<a name="179414540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179414540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179414540">(Oct 30 2019 at 09:28)</a>:</h4>
<p>we have a crate that uses a <code>.a</code> produced by nasm</p>



<a name="179414576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179414576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179414576">(Oct 30 2019 at 09:29)</a>:</h4>
<p>and it references symbols that are actually defined in the rust code</p>



<a name="179414728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179414728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179414728">(Oct 30 2019 at 09:31)</a>:</h4>
<p>Can you try adding <code>#[used]</code> to <code>rav1e_dr_intra_derivative</code> in <code>tables.rs</code>? It might have been optimized out as an unreachable private symbol.</p>



<a name="179415008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179415008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179415008">(Oct 30 2019 at 09:35)</a>:</h4>
<p>when building non <code>cdylib</code> everything works</p>



<a name="179415015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179415015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179415015">(Oct 30 2019 at 09:35)</a>:</h4>
<p>so seems unlikely</p>



<a name="179415047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179415047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179415047">(Oct 30 2019 at 09:35)</a>:</h4>
<p>I should check the zulip mobile app =/</p>



<a name="179415053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179415053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179415053">(Oct 30 2019 at 09:35)</a>:</h4>
<p>I must run</p>



<a name="179415179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179415179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179415179">(Oct 30 2019 at 09:37)</a>:</h4>
<p>I'll follow up for Luca</p>



<a name="179417102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179417102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179417102">(Oct 30 2019 at 10:06)</a>:</h4>
<p>So, <code>#[used]</code> gave the same error.<br>
I did manage to work around by first building the static lib and then replacing the static nasm archive link argument with that.</p>



<a name="179419058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179419058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179419058">(Oct 30 2019 at 10:38)</a>:</h4>
<p>The linker on macOS does not fail in this way. Testing must be done on Linux.</p>



<a name="179419083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179419083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179419083">(Oct 30 2019 at 10:38)</a>:</h4>
<p>The underlying issue is that 2 static libs are mutually dependent.</p>



<a name="179425755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179425755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179425755">(Oct 30 2019 at 12:23)</a>:</h4>
<p>Some linkers might require <code>-lA -lB -lA</code></p>



<a name="179429223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179429223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179429223">(Oct 30 2019 at 13:10)</a>:</h4>
<p><span class="user-mention" data-user-id="247214">@David Michael Barr</span> do you have a self contained example that shows the issue? (a github repo or so?)</p>



<a name="179429258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179429258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179429258">(Oct 30 2019 at 13:10)</a>:</h4>
<p>I've used nasm on macosx and linux and called into it from Rust code, and I think I ran into similar issues</p>



<a name="179429304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179429304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179429304">(Oct 30 2019 at 13:11)</a>:</h4>
<p>I don't remember exactly how I solved them, just that I had to play tetris with linker and nasm options</p>



<a name="179429324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179429324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179429324">(Oct 30 2019 at 13:11)</a>:</h4>
<p>but I do recall that -fPIC error and using -fPIC not solving my problem <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span></p>



<a name="179429511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179429511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179429511">(Oct 30 2019 at 13:13)</a>:</h4>
<p>I think the fPIC error is actually a secondary effect. The symbol is not being resolved as intended, causing a mismatch with respect to PIC.</p>



<a name="179429885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179429885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179429885">(Oct 30 2019 at 13:17)</a>:</h4>
<p>Non-trivial reproduction, assuming nasm installed:</p>
<div class="codehilite"><pre><span></span>git clone https://github.com/xiph/rav1e
cd rav1e
cargo install cargo-c
cargo cbuild --release
</pre></div>



<a name="179430097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179430097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179430097">(Oct 30 2019 at 13:19)</a>:</h4>
<p>Non-trivial reproduction, assuming nasm installed:</p>
<div class="codehilite"><pre><span></span>git clone https://github.com/xiph/rav1e
cd rav1e
cargo install cargo-c
cargo cbuild --release
</pre></div>



<a name="179437771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179437771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179437771">(Oct 30 2019 at 14:33)</a>:</h4>
<p><a href="https://github.com/barrbrain/cargo-c-nasm-repro" target="_blank" title="https://github.com/barrbrain/cargo-c-nasm-repro">https://github.com/barrbrain/cargo-c-nasm-repro</a></p>



<a name="179438126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Accessing%20symbols%20from%20nasm/near/179438126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Michael Barr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Accessing.20symbols.20from.20nasm.html#179438126">(Oct 30 2019 at 14:37)</a>:</h4>
<p>I have reduced the problem to its essence.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>